package com.clementheliou.cheliou.web.controller.gallery.photo.model;

import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.clementheliou.cheliou.business.dataaccess.photo.PhotoDataAccessService;
import com.clementheliou.cheliou.dal.model.base.enumeration.SortOrder;
import com.clementheliou.cheliou.dal.model.comment.Comment;
import com.clementheliou.cheliou.dal.model.photo.dto.PhotoDTO;
import com.clementheliou.cheliou.dal.model.photo.enumeration.PhotoSortableColumn;

/**
 * <i>Primefaces</i> {@link LazyDataModel} dedicated to the photo's data grid.
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link Comment};
 * @see {@link Component};
 * @see {@link LazyDataModel};
 * @see {@link PhotoDTO};
 * @see {@link Scope}.
 * @since 1.0
 */
@Component
@Scope("session")
public class PhotoLazyDataModel extends LazyDataModel<PhotoDTO> {

	/**
	 * Logger for this class.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Logger};
	 * @see {@link LoggerFactory}.
	 * @since 1.0
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(PhotoLazyDataModel.class);

	/**
	 * UID for serialization.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private static final long serialVersionUID = -2800401334455735500L;

	/**
	 * Column to be used to sort the model content. The default value is
	 * {@link PhotoSortableColumn#ADDITION_DATE}.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private PhotoSortableColumn sortableColumn = PhotoSortableColumn.ADDITION_DATE;

	/**
	 * Order to be used to sort the model content. The default value is {@link SortOrder#DESCENDING}
	 * .
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private SortOrder currentSortOrder = SortOrder.DESCENDING;

	/**
	 * Data access service about the photos.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Autowired
	private transient PhotoDataAccessService photoDataAccessService;

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public List<PhotoDTO> load(int first, int pageSize, String sortField, org.primefaces.model.SortOrder sortOrder, Map<String, String> filters) {
		LOGGER.debug("Lazy loading of the photos with first {} and page size of {}.", first, pageSize);

		Assert.notNull(currentSortOrder, "A sort order must be provided at this time.");
		Assert.notNull(sortableColumn, "A sort column must be provided at this time.");

		setRowCount(photoDataAccessService.countPhotos());
		return photoDataAccessService.getLastPhotos(first, pageSize, sortableColumn, currentSortOrder);
	}

	/*
	 * Getters & setters.
	 */

	public PhotoSortableColumn getSortableColumn() {
		return sortableColumn;
	}

	public void setSortableColumn(PhotoSortableColumn sortableColumn) {
		this.sortableColumn = sortableColumn;
	}

	public SortOrder getCurrentSortOrder() {
		return currentSortOrder;
	}

	public void setCurrentSortOrder(SortOrder currentSortOrder) {
		this.currentSortOrder = currentSortOrder;
	}

}
